package com.niu.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.niu.crm.dao.mapper.UserFuncMapper;
import com.niu.crm.form.UserFuncSearchForm;
import com.niu.crm.model.Unit;
import com.niu.crm.model.User;
import com.niu.crm.model.UserFunc;
import com.niu.crm.service.BaseService;
import com.niu.crm.service.UnitService;
import com.niu.crm.service.UserFuncService;
import com.niu.crm.service.DictService;
import com.niu.crm.service.UserService;

@Service
public class UserFuncServiceImpl extends BaseService implements UserFuncService {
    @Autowired
    private UserFuncMapper  userFuncMapper;
    @Autowired
    private UnitService unitService;
    @Autowired
    private UserService userService;
    @Autowired
    private DictService dictService;

    @Transactional
    @Override
    /**
     * @Param user 操作人
     * @userId 被设置人
     */
	public void setUserFunc(User user, Long userId, List<UserFunc> lsUserFunc) {
    	userFuncMapper.deleteByUserId(userId);
    	for(int i=0; lsUserFunc !=null && i < lsUserFunc.size(); i++){
    		UserFunc userFunc = lsUserFunc.get(i);
    		userFunc.setUserId(userId);
    		userFunc.setCreatorId(user.getId());
    		
    		userFuncMapper.insert(userFunc);
    	}
	}
    
	@Override
	public int add(UserFunc userFunc) {
		return userFuncMapper.insert(userFunc);
	}

	@Override
	public void delete(Long userId, Long id) {
		userFuncMapper.delete(userId, id);
	}

	@Override
	public List<UserFunc> queryUserFunc(UserFuncSearchForm form) {
		return userFuncMapper.queryUserFunc(form);
	}
	
	@Override
	public UserFunc loadByCode(Long userId, String funcCode){
		UserFuncSearchForm form =new UserFuncSearchForm();
		form.setUserId(userId);
		form.setFuncCode(funcCode);
		
		List<UserFunc> ls = userFuncMapper.queryUserFunc(form);
		if(ls == null || ls.size() ==0)
			return null;
		else
			return ls.get(0);
		
	}
	
	@Override
	public boolean hasFunc(Long userId, String funcCode){
		UserFunc vo = loadByCode(userId, funcCode);
		
		if(vo == null )
			return false;
		else
			return true;
	}
	

	@Override
	public List<UserFunc> loadAll(Long userId) {
		return userFuncMapper.selectAll(userId);
	}
	

}
